<?php
/**
 * 飞信机器人--功能插件--活动参与
 * 功能：可以实现抽奖、组织活动等
 * 数据表：fx_participate
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/
/*
支持参数(fx_menus表 extra_params字段)：
eventid=事件id
action=show:显示每天自动抽奖名单
*/
/*
 数据表定义:
CREATE TABLE `fx_participate` (
  `id` int(14) NOT NULL auto_increment,
  `buddy_id` int(14) NOT NULL,
  `event_id` int(8) NOT NULL COMMENT '事件id',
  `add_time` int(14) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
*/

class Participate extends CommonPlugin
{
	var $UUID='c11b783b-d8a2-4dfb-b68f-0a0d6adada4b';
	
	public function Participate(&$session, $pid, $extraPara="")
	{
		parent::__construct($session, $pid, $extraPara);
	}
	
	private function getEventQuestion($class_id)
	{
		$eventid = $this->getValue("{$class_id}_question");
		return $eventid;
	}
	
	private function setEventQuestion($class_id, $question)
	{
		$this->set("{$class_id}_question", $question);
	}
	private function getRaffleDay($eventid)
	{
		$day = $this->getStr("{$eventid}_raffleday");
		if( $day == '' )
		{
			$day = date("Y-m-d",time()-24*60*60);
		}
		return $day;
	}
	private function setRaffleDay($eventid, $day)
	{
		$this->set("{$eventid}_raffleday", $day);
	}
	public function getGuide($eventid,$day, &$hasPre, &$hasNext)
	{
		$sql = "select distinct(raffle_time) from fx_auto_raffle where event_id={$eventid} order by raffle_time desc";
		$rows = $this->query($sql);
		$hasPre = false;
		$hasNext = false;
		
		if(count($rows)==0)return "";
		
		//有前一天的数据
		if( $day < $rows[0]['raffle_time'] )$hasPre = true;
		if( $day > $rows[count($rows)-1]['raffle_time'])$hasNext = true;
		
		$msg = "";
		if($hasPre)$msg.=",p--前一天";
		if($hasNext)$msg.=",n--下一天";
		
		return $msg;
	}
	public function getRaffleList($eventid)
	{
		$day =  $this->getRaffleDay($eventid);
		$sql = "select a.buddy_id,b.sid,b.mobile,b.nick_name from fx_auto_raffle a inner join fx_buddies b on a.buddy_id=b.id where a.event_id={$eventid} and a.raffle_time='{$day}'";
		$rows = $this->query($sql);
		if(count($rows)==0)
		{
			$hasPre = false;
			$hasNext = false;
			return "{$day} 没有开奖记录\n".$this->getFooter($this->getGuide($eventid,$day,$hasPre, $hasNext));
		}
		
		//$lastDay =  date("Y年m月d日中奖名单(每日0点抽取前一天的中奖名单)",time()-24*60*60); 
		$msg = "{$day}中奖名单\n";
		for($i=0; $i<count($rows); $i++ )
		{		
			$msg.= "飞信号:{$rows[$i]['sid']}\n";
		}
		$hasPre = false;
		$hasNext = false;
		return $msg.$this->getFooter($this->getGuide($eventid,$day,$hasPre, $hasNext));
	}
	public function getTips()
	{
		$eventid=$this->extraParam['eventid'];
		$action=$this->extraParam['action'];
		if($action=='show')
		{
			$this->setRaffleDay($eventid,'');
			return $this->getRaffleList($eventid);
		}

		$sql = "select count(*) as count from fx_participate where event_id={$eventid}";
		$retArr = $this->query($sql);
		$count = $retArr[0]['count'];	
		$msg = "已有 {$count} 人参与本次活动！";
		return $this->session->currentMenu['tips']."\n".$msg."\n".$this->getFooter("");
	}

	public function handleShow($eventid,$input)
	{
		$day =  $this->getRaffleDay($eventid);
		$hasPre = false;
		$hasNext = false;
		$menuExtra = $this->getGuide($eventid,$day,$hasPre, $hasNext);
		if( $input!='p' && $input != 'n' )return "无效输入\n".$this->getFooter($menuExtra);
		
		if( !$hasPre && $input=='p' || !$hasNext && $input=='n' )return "无更多数据\n".$this->getFooter($menuExtra);
		
		$dayArr = explode('-',$day);
		// 前一天
		if($input == 'p')
		{
			$year = intval($dayArr[0]);
			$month = intval($dayArr[1]);
			$day = intval($dayArr[2]);
			$day = date("Y-m-d", mktime(0, 0, 0, $month, $day+1, $year));
		}
		// 下一天
		if($input == 'n')
		{
			$year = intval($dayArr[0]);
			$month = intval($dayArr[1]);
			$day = intval($dayArr[2]);
			$day = date("Y-m-d", mktime(0, 0, 0, $month, $day-1, $year));
		}
		$this->setRaffleDay($eventid,$day);
		return $this->getRaffleList($eventid);
	}
	public function handle($input)
	{
		if( parent::handleInput($input)) return false;
		
		$eventid=$this->extraParam['eventid'];
		$action=$this->extraParam['action'];
		
		if( $action == 'show' )return $this->handleShow($eventid,$input);
		
		$msg = "";
		if( $input== "我要参与" )
		{
			$today = date("Y-m-d");
			$sql = "select * from fx_participate where event_id={$eventid} and buddy_id={$this->session->buddyID} and left(from_unixtime(add_time),10)='{$today}'";
			$retArr = $this->query($sql);
			if(count($retArr) >= 1)
			{
				$time = date('Y年m月d日H时i分s秒', $retArr[0]['add_time']);
				$msg = "您已经于{$time}参加“我要抢票”活动，请改日参加！";
			}else 
			{
				$time = time(NULL);
				$sql = "insert into fx_participate(buddy_id,event_id,add_time) values({$this->session->buddyID},{$eventid},{$time})";
				$this->execute($sql);
				$msg = "您已成功报名，飞信精灵将于次日0点00分进行随机抽奖，获奖结果将公布在“每日幸运榜”中，感谢您的参与！\n";
			}
		}	
		return $msg.$this->getFooter();
	}
	
}
?>